package com.android.picotts.ui.activity.pick;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.android.picotts.R;
import com.android.picotts.businiss.SpriterManager;
import com.android.picotts.businiss.model.Spriter;
import com.android.picotts.businiss.model.SpriterType;
import com.android.picotts.ui.activity.common.TemplateActivity;
import com.android.picotts.ui.widget.TitleBar;
import com.android.picotts.ui.widget.TitleBar.OnTitleClickListener;

public class PickWebActivity extends TemplateActivity implements OnClickListener{
    private WebView webView;
    private ProgressBar webProcessBar;
    private View web_lay,suer_lay;
    private EditText lableEt;
    private String curUrl;
    
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setNoTitle();
        setContentView(R.layout.activity_pickweb);
        hardwareAccelerate();
        Uri uri = getIntent().getData();
        if(uri != null) {
            curUrl = uri.toString();
        }
        
        webView = getViewById(R.id.webView);
        webProcessBar = getViewById(R.id.webProcessBar);
        web_lay = getViewById(R.id.web_lay);
        suer_lay = getViewById(R.id.suer_lay);
        lableEt = getViewById(R.id.lableEt);
        
        WebSettings settings = webView.getSettings();  
        settings.setSupportZoom(true);          //支持缩放  
        settings.setBuiltInZoomControls(false);  //启用内置缩放装置       
        settings.setJavaScriptEnabled(true);    //启用JS脚本
        settings.setDomStorageEnabled(true);    //启用JS脚本
        settings.setPluginsEnabled(true);    //启用JS脚本
        webView.requestFocus();
        
        //以下两句和硬件加速有关  
        settings.setPluginState(PluginState.ON);
        settings.setAllowFileAccess(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        
        webView.setWebViewClient(mClient);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    webProcessBar.setVisibility(View.GONE);
                } else {
                    if (webProcessBar.getVisibility() == View.GONE)
                        webProcessBar.setVisibility(View.VISIBLE);
                    webProcessBar.setProgress(newProgress);
                }
            }
        });
        if(curUrl != null) {
            webView.loadUrl(curUrl);
        }
    }
    
    @Override
    protected void onPause() {
        try {  
            if (webView != null) {  
                webView.getClass().getMethod("onPause").invoke(webView, (Object[]) null);
            } 
        } catch (Exception e) {  
            e.printStackTrace();  
        }
        super.onPause();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        try {
             if (webView != null) {
                 webView.getClass().getMethod("onResume").invoke(webView, (Object[]) null);  
             }
        } catch (Exception e) {
            e.printStackTrace();  
        }
    }
    
    @Override
    protected void onDestroy() {
        mContainer.removeAllViews();
        webView.clearHistory();
        webView.clearCache(true);
        webView.loadUrl("about:blank");
        webView.freeMemory(); 
        webView.pauseTimers();
        webView = null; 
        super.onDestroy();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar) {
        titleBar.setTitle("选择网址");
        titleBar.setRightVisible(true);
        
        titleBar.setOnTitleClickListener(new OnTitleClickListener() {
            @Override
            public void onRightClick() {
                if(web_lay.getVisibility() == View.VISIBLE) {
                    web_lay.setVisibility(View.GONE);
                    suer_lay.setVisibility(View.VISIBLE);
                }else {
                    web_lay.setVisibility(View.VISIBLE);
                    suer_lay.setVisibility(View.GONE);
                }
            }
        }, false, true);
    }
    
    @Override
    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
            return;
        }
        super.onBackPressed();
    }
    
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btn_ok:{
                String name = lableEt.getText().toString();
                SpriterManager.getInstance(this).add(new Spriter(SpriterType.WEB, name, curUrl));
                finish();
                break;
            }
        }
    }
    
    private void hardwareAccelerate(){  
        if (this.getPhoneSDKInt() >= 14) {  
            getWindow().setFlags(0x1000000, 0x1000000);  
        }  
    } 
    
    public int getPhoneSDKInt() {  
        int version = 0;  
        try {  
            version = Integer.valueOf(android.os.Build.VERSION.SDK);  
        } catch (NumberFormatException e) {  
            e.printStackTrace();  
        }  
        return version;  
    }  
    
    private WebViewClient mClient = new WebViewClient() {
        
        public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
            
        }
        
        public void onPageFinished(WebView view, String url) {
            Log.d("pick web", url);
            curUrl = url;
        }
        
        public boolean shouldOverrideUrlLoading(WebView view, String url) {            
            return false;
        }
    };
    
    public static void startActivity(Activity activity, String url) {
        Intent it  = new Intent(activity, PickWebActivity.class);
        it.setData(Uri.parse(url));
        activity.startActivity(it);
    }
}